package com.zpl.order.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

@Slf4j
@Component
public class MemberIdCacheKeyGenerator implements KeyGenerator {
    @Override
    public Object generate(Object o, Method method, Object... objects) {
        log.info(o.getClass().toGenericString());
        log.info(method.getName());
        String str = null;
        for (Object obj : objects){
            try {
                Field field = obj.getClass().getDeclaredField("memberId");
                field.setAccessible(true);
                if(null!=field){
                    str = field.get(obj).toString();
                }
            }catch (NoSuchFieldException | IllegalAccessException e){
                e.printStackTrace();
            }
        }
        return str;
    }
}
